package com.moli.iov.down.command.mq.kafka.handle;

import com.moli.iov.down.command.design.command.reshandler.ResponseHandlerCommand;
import com.moli.iov.down.command.design.factory.ReponseHandlerFactory;
import com.moli.iov.down.command.codec.utils.Protocol;
import com.moli.iov.down.command.codec.utils.ProtocolAnalysisUtil;
import com.moli.iov.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * <p>
 *
 * </p>
 *
 * @author: zql
 * @Date: 2018/12/29 17:53
 */
@Slf4j
@Component
public class ResponseHandle {

    /**
     * 终端响应Handle
     * @param vin
     * @param allData
     */
    public void responseHandle(String vin, byte[] allData){

        Protocol protocol = ProtocolAnalysisUtil.decode(allData);

        if(protocol == null){
            log.error("[上行响应报文] 协议解析异常 protocol为Null,  vin：{} ， datas: {}", vin, allData);
            return;
        }

        String data = protocol.getData() == null ? "" : StringUtil.bytesToHexString(protocol.getData());
        log.info("[上行响应报文] vin: {}, 命令：{}， 报文:{}",
                protocol.getVin(),
                StringUtil.bytesToHexString(protocol.getCommandTag()),
                data);

        ResponseHandlerCommand responseHandlerCommand = ReponseHandlerFactory.getResponseHandlerCommand(protocol.getCommandTag());

        if(responseHandlerCommand == null){
            log.error("[上行响应报文] 终端响应业处理 handle  业务处理 失败，" +
                            "未定义协议， vin:{} , 命令：{}，protocol: {}",
                    protocol.getVin(), protocol.getCommandTag(), protocol);
            return;
        }

        responseHandlerCommand.execute(protocol);
    }
}
